<template>
  <div id="UpdatePasswd">
       <!--    导航栏 -->
         <van-nav-bar
            title="用户密码修改"
            left-arrow
            :fixed=true
            :border=true
            @click-left="$router.go(-1)"
         ></van-nav-bar>
         <div style="margin-top:3rem;">

             <van-field v-model="oldpasswd" type="password" label="旧密码" />
             <van-field maxlength='12' v-model="newpasswd" type="password" label="新密码" />
             <van-field maxlength='12' v-model="cnewpasswd" type="password" label="确认新密码" />
              <div style="margin: 16px;">
                <van-button round block type="primary" @click="onSubmit()">
                  更新保存
                </van-button>
              </div>
         </div>
  </div>
</template>

<script>
import { DatetimePicker, NoticeBar ,Toast} from 'vant'
import { getUserByPasswd,UpdatePasswd } from './../../../../service/api/index.js'
import {mapState,mapMutations} from 'vuex'
  export default{
     name:"Pay",
     data(){
         return{
            oldpasswd:'',
            newpasswd:'',
            cnewpasswd:'',
         }
     },
      
      mounted(){
        //console.log(this.$route);
        // console.log(this.userInfo.token)
      },

      computed:{
          ...mapState(["userInfo"]),

     },
     methods:{

         async onSubmit() {
               if (this.oldpasswd.length===0) {
                   Toast.fail('请输入旧密码验证身份!');
                   return ;
               }
               //判断旧密码是否正确
               let res = await getUserByPasswd(this.userInfo.token,this.oldpasswd);
             //  console.log(res);
               if (res.status==='500') {
                   Toast.fail('旧密码错误，请重新输入!');
                   return ;
               }
               //密码不能少于6位
              if (this.newpasswd.length<6) {
                  Toast.fail('密码不能少于6位!');
                   return ;
              };
              if (this.newpasswd!=this.cnewpasswd) {
                  Toast.fail('确认密码不正确!');
                  return ;
              }

              //修改密码
               let result = await UpdatePasswd(this.userInfo.token,this.newpasswd);
               console.log(result);
               if (result.status==='200') {
                   //提示更新更改
                  Toast.success('密码修改成功');
               }
        },

          
     }

  }
</script>


<style lang="less" scoped>
     #UpdatePasswd{
      position:fixed;
      top:0;
      left:0;
      bottom:0;
      right:0;
      background-color:#f5f5f5;
      z-index:200;
      overflow-y: scroll;

     }


    .router-slider-enter-active,.router-slider-leave-active{
      transition:all 0.3s;
    }

    .router-slider-enter,.router-slider-leave-active{
      transform:translate3d(2rem,0,0);
      opacity:0;
    }
</style>